package cn.tworice.upload.service;

import cn.tworice.common.util.file.FileUtils;
import cn.tworice.upload.client.FileClient;
import cn.tworice.upload.constand.UploadConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 本地文件上传
 */

@Slf4j
@Component
public class UploadService {

    private final FileClient fileClient;

    @Autowired
    public UploadService(@Lazy FileClient fileClient) {
        this.fileClient = fileClient;
    }

    /**
     * 上传文件到本地upload目录下
     * @return 返回磁盘上的文件名称
     * @author 二饭
     */
    public String uploadFile(MultipartFile file) {
        try {
            return fileClient.upload(file);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 上传文件到指定目录
     * @param file 文件
     * @param dir 目标目录
     * @return 返回磁盘上的文件名称
     */
    public String uploadFileToDir(MultipartFile file,String dir) {
        try {
            return fileClient.uploadToPath(file, dir);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public String upload(BufferedImage image) {
        try {
            return fileClient.upload(image);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 将文件拷贝到项目static下 避免target目录删除后文件丢失
     * @param filePath target文件路径
     **/
    @Deprecated
    private void copy(String filePath,String file) {
        File sourceFile = new File(filePath+file);

        String localPath = System.getProperty("user.dir") + UploadConst.srcStaticPath;
        try {
            File targetFile = new File(localPath+"upload");
            if (!targetFile.exists()) {
                log.debug("目标文件夹不存在，进行创建:"+localPath+"upload");
                targetFile.mkdirs();
            }
            FileUtils.copyFileUsingFileChannels(sourceFile,new File(localPath+file));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 上传文件到指定路径
     * 只能上传到项目目录中
     */
    @Deprecated
    public String uploadFile(MultipartFile file, String targetPath) {
        String path = null;
        try {
            if (file == null || file.getOriginalFilename()==null) {
                return null;
            }
            //获取文件后缀
            String fileExt = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1)
                    .toLowerCase();
            String pikId = UUID.randomUUID().toString().replaceAll("-", "");
            path = "/"+targetPath+"/" + pikId + "." + fileExt;
            String upload=System.getProperty("user.dir")+"\\target\\classes\\static\\";
            file.transferTo(new File(upload+path));
        }catch (Exception e){
            e.printStackTrace();
            log.error("上传文件失败");
            return null;
        }
        return path;
    }

    /**
     * 批量上传文件
     * @param files 要上传的文件列表
     * @return 路径列表
     */
    public List<String> uploadFiles(MultipartFile[] files) {
        List<String> paths = new ArrayList<>();
        for (MultipartFile file : files) {
            paths.add(this.uploadFile(file));
        }
        return paths;
    }
}
